package com.lune.job.controller;


import com.lune.job.pojo.Experience;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.service.IExperienceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/experience")
@Api(tags = "工作经历管理")
public class ExperienceController {
    @Autowired
    private IExperienceService experienceService;

    @ApiOperation(value = "获取所有工作经历")
    @GetMapping("/")
    public List<Experience> getAllDict() {
        return experienceService.list();
    }

    @ApiOperation(value = "添加工作经历")
    @PostMapping("/")
    public RespBean create(@RequestBody Experience experience) {
        if (experienceService.save(experience)) {
            return RespBean.success("添加工作经历成功");
        } else {
            return RespBean.error("添加工作经历失败");
        }
    }

    @ApiOperation(value = "删除单个工作经历")
    @DeleteMapping("/{id}")
    public RespBean deleteById(@PathVariable Integer id) {
        if (experienceService.removeById(id)) {
            return RespBean.success("删除工作经历成功");
        }
        return RespBean.error("删除工作经历失败");
    }

    @ApiOperation(value = "批量删除工作经历")
    @DeleteMapping("/")
    public RespBean delete(Integer[] ids) {
        if (experienceService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除工作经历成功");
        } else {
            return RespBean.error("批量删除工作经历失败");
        }
    }

    @ApiOperation(value = "修改工作经历")
    @PutMapping("/")
    public RespBean update(@RequestBody Experience experience) {
        if (experienceService.updateById(experience)) {
            return RespBean.success("修改工作经历成功");
        } else {
            return RespBean.error("修改工作经历失败");
        }
    }

    @ApiOperation(value = "根据id查询工作经历")
    @GetMapping("/detail")
    public Experience detail(Integer id) {
        return experienceService.getById(id);
    }

    @ApiOperation(value = "分页查询工作经历")
    @PostMapping("/page")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        @RequestBody Experience experience) {
        return experienceService.queryPage(currentPage, size, experience);
    }

    @ApiOperation(value = "条件查询工作经历")
    @PostMapping("/query")
    public List<Experience> queryDict(
            @RequestBody Experience experience) {
        return experienceService.queryByCon(experience);
    }


}
